<template>
  <teleport to="body">
    <div v-if="isVisible" class="confirm" @click.self="close">
      <div class="dialog">
        <div class="X-box">
          <h3 v-html="title"></h3>
          <div @click="close" class="close-button">×</div>
        </div>
        <div class="content" v-html="content"></div>
        <div class="operation">
          <!-- <button style="margin-right: 8px" @click="close">取消</button> -->
          <button class="action-blue" @click="close">确定</button>
        </div>
      </div>
    </div>
  </teleport>
</template>

<script setup>
import { ref, defineExpose } from 'vue'

const isVisible = ref(false)
const title = ref('')
const content = ref('')

const open = (options = {}) => {
  title.value = options.title || '提示'
  content.value = options.content || ''
  isVisible.value = true
}

const close = () => {
  isVisible.value = false
}

defineExpose({
  open,
  close
})
</script>

<style scoped>
.confirm {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.3);
  z-index: 999;
  color: black;
}

.confirm .dialog {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: white;
  width: 58%;
  max-width: 420px;
  padding: 16px;
  border-radius: 8px;
}

.confirm .dialog .content {
  padding: 16px 0;
  white-space: normal; /* 默认值，可不写 */
  word-wrap: break-word; /* 过时写法，兼容老浏览器 */
  overflow-wrap: break-word; /* 现代标准 */
  word-break: break-word; /* 中英文混合场景 */
  /* padding: 8px 0 16px 0; */
}

.confirm .dialog .operation {
  padding-top: 8px;
  display: flex;
  justify-content: center;
}

.confirm .dialog .X-box {
  position: relative;
}

.confirm .dialog .X-box h3 {
  margin-top: -10px;
  /* margin-left: -4px; */
}

.confirm .dialog .X-box .close-button {
  position: absolute;
  top: 0;
  right: -10px;
  width: 16px;
  height: 16px;
  font-size: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

button {
  margin: 0 8px;
  padding: 5px 12px;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s ease;
  -webkit-tap-highlight-color: transparent;
}

button:hover {
  opacity: 0.8;
  color: #aaa;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.action-blue {
  background-color: #3498db;
  color: white;
}

.action-red {
  background-color: #e74c3c;
  color: white;
}
</style>
